package org.zachary.agent.domain.aggregate_model.user;

import jakarta.persistence.Embeddable;
import lombok.Getter;
import java.io.Serial;
import java.io.Serializable;
import java.util.Objects;
import java.util.regex.Pattern;

@Getter
@Embeddable
public class ContactUs implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;


    private static final String PHONE_REGEX =
            "^(?:(?:1(?:3[0-9]|4[579]|5[0-35-9]|6[6]|7[0-35-9]|8[0-9]|9[89]))\\d{8})$" +
                    "|(?:1(?:4[0-9]|7[0-9]|9[0-9])\\d{8})$" +
                    "|(?:1(?:45|49|70|72|74|76|77|78|79|82|88|99)\\d{7})$";


    private static final String EMAIL_REGEX =
            "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+(?:\\.[a-zA-Z0-9_!#$%&'*+/=?`{|}~^-]+)*@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$";

    private static final Pattern PHONE_PATTERN = Pattern.compile(PHONE_REGEX);
    private static final Pattern EMAIL_PATTERN = Pattern.compile(EMAIL_REGEX);

    private String phone;
    private String email;

    public ContactUs(String email, String phone) {
        if(email == null || phone == null || email.isEmpty() || phone.isEmpty()) {
            throw new IllegalArgumentException("邮箱和手机号不能为空");
        }
//        if(!isValid()){
//            throw new IllegalArgumentException("邮箱或密码不合法");
//        }
        this.email = email;
        this.phone = phone;
    }

    private boolean validateEmail() {
        return EMAIL_PATTERN.matcher(email).matches();
    }

    private boolean validatePhone() {
        return PHONE_PATTERN.matcher(phone).matches();
    }

    protected ContactUs() {}

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ContactUs contactUs = (ContactUs) o;
        return Objects.equals(email, contactUs.email) &&
                Objects.equals(phone, contactUs.phone);
    }

    @Override
    public int hashCode() {
        return Objects.hash(email, phone);
    }

    public boolean isValid() {
        return validateEmail() && validatePhone();
    }

    @Override
    public String toString() {
        return String.join(",", email, phone);
    }
}